﻿using System.Configuration;
using System.Web;
using System.Web.Configuration;

namespace Actya.Web.Infrastructure
{
	public static class HostUtils
	{
		public static AspNetHostingPermissionLevel GetCurrentTrustLevel()
		{
			foreach (AspNetHostingPermissionLevel trustLevel in new[] {
                AspNetHostingPermissionLevel.Unrestricted,
                AspNetHostingPermissionLevel.High,
                AspNetHostingPermissionLevel.Medium,
                AspNetHostingPermissionLevel.Low,
                AspNetHostingPermissionLevel.Minimal 
            })
			{
				try
				{ 
					new AspNetHostingPermission(trustLevel).Demand();
				}
				catch (System.Security.SecurityException)
				{
					continue;
				}

				return trustLevel;
			}

			return AspNetHostingPermissionLevel.None;
		}

		public static int GetMaxRequestSize()
		{
			var httpRuntimeSection = WebConfigurationManager.GetSection("system.web/httpRuntime", "~") as HttpRuntimeSection;
			if (httpRuntimeSection != null)
			{
				return httpRuntimeSection.MaxRequestLength;
			}
			return 0;
		}
	}
}